package com.woniu.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.ResponseEnum;
import com.woniu.server.mapper.ProjectStorageMapper;
import com.woniu.server.pojo.ProjectStorage;
import com.woniu.server.service.ProjectStorageService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author a
* @description 针对表【project_storage(产品存储表)】的数据库操作Service实现
* @createDate 2024-11-22 09:37:51
*/
@Service
public class ProjectStorageServiceImpl extends ServiceImpl<ProjectStorageMapper, ProjectStorage>
    implements ProjectStorageService{

    @Override
    public ResponseData<?> reduceStock(String productCode, Integer reduceNum) {
        ProjectStorage one = lambdaQuery().eq(ProjectStorage::getProductCode, productCode).one();
        if(!Objects.isNull(one)) {
            if(reduceNum > one.getQuantity()) {
                return new ResponseData().fail(ResponseEnum.STOCK_NOT_ENOUGH);
            } else {
                one.setQuantity(one.getQuantity() - reduceNum);
                lambdaUpdate().eq(ProjectStorage::getProductCode, one.getProductCode())
                        .set(ProjectStorage::getQuantity, one.getQuantity())
                        .update();
                return new ResponseData().ok();
            }
        }
       return new ResponseData().fail(ResponseEnum.PRODUCT_NOT_EXIST);
    }

    @Override
    public ResponseData<?> rollbackStock(String productCode, Integer reduceNum) {
        ProjectStorage one = lambdaQuery().eq(ProjectStorage::getProductCode, productCode).one();
        one.setQuantity(one.getQuantity() + reduceNum);
        lambdaUpdate().eq(ProjectStorage::getProductCode, one.getProductCode())
                .set(ProjectStorage::getQuantity, one.getQuantity())
                .update();
        return new ResponseData().ok();
    }
}




